package userInterface;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import controller.Controller;

import domain.Movie;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JEditorPane;
import org.jdesktop.beansbinding.ObjectProperty;
import javax.swing.DropMode;

public class RateDialog extends JDialog {
	private Controller controller;
	private final JPanel contentPanel = new JPanel();
	private JTextField moviename_field;
	private Movie movietoRate;
	private JSpinner spinner ;
	private JEditorPane editorPane;
	
	/**
	 * Create the dialog.
	 */
	public RateDialog(Movie movieToBeRated, final Controller controller) {
		this.controller = controller;
		this.movietoRate = movieToBeRated;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("Name");
			lblNewLabel.setBounds(10, 47, 110, 30);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("Descryption");
			lblNewLabel_1.setBounds(10, 88, 110, 30);
			contentPanel.add(lblNewLabel_1);
		}
		{
			moviename_field = new JTextField();
			moviename_field.setBounds(130, 47, 213, 30);
			contentPanel.add(moviename_field);
			moviename_field.setColumns(10);
		}
		{
			JLabel lblNewLabel_2 = new JLabel("Rate");
			lblNewLabel_2.setBounds(10, 171, 110, 30);
			contentPanel.add(lblNewLabel_2);
		}
		{
			SpinnerModel model = new SpinnerNumberModel(0, 0, 5, 1);
			spinner = new JSpinner(model);
			spinner.setBounds(130, 171, 45, 30);
			contentPanel.add(spinner);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			scrollPane.setBounds(130, 88, 213, 69);
			contentPanel.add(scrollPane);
			{
				editorPane = new JEditorPane();
				scrollPane.setViewportView(editorPane);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Rate");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						try {
							int value = Integer.parseInt(spinner.getValue().toString());
							controller.giveRating(movietoRate.getMovieDescription().getMovieId(), value);
							setVisible(false);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		initDataBindings();
	}
	protected void initDataBindings() {
		BeanProperty<Movie, String> movieBeanProperty = BeanProperty.create("movieDescription.name");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<Movie, String, JTextField, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, movietoRate, movieBeanProperty, moviename_field, jTextFieldBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<Movie, String> movieBeanProperty_1 = BeanProperty.create("movieDescription.description");
		BeanProperty<JEditorPane, String> jEditorPaneBeanProperty = BeanProperty.create("text");
		AutoBinding<Movie, String, JEditorPane, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ, movietoRate, movieBeanProperty_1, editorPane, jEditorPaneBeanProperty);
		autoBinding_1.bind();
	}
}
